package com.javaxiaobear.base.framework.interceptor.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.json.JSONUtil;
import com.javaxiaobear.base.common.constant.CacheConstants;
import com.javaxiaobear.base.common.filter.RepeatedlyRequestWrapper;
import com.javaxiaobear.base.common.utils.StringUtils;
import com.javaxiaobear.base.common.utils.http.HttpHelper;
import com.javaxiaobear.base.framework.interceptor.RepeatSubmitInterceptor;
import com.javaxiaobear.base.framework.interceptor.annotation.RepeatSubmit;
import com.javaxiaobear.base.framework.redis.RedisCache;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.springframework.stereotype.Component;

/**
 * 判断请求url和数据是否和上一次相同， 如果和上次相同，则是重复提交表单。 有效时间为10秒内。
 *
 * @author javaxiaobear
 */
@Component
public class SameUrlDataInterceptor extends RepeatSubmitInterceptor {
  public final String REPEAT_PARAMS = "repeatParams";

  public final String REPEAT_TIME = "repeatTime";

  @Resource RedisCache redisCache;

  @SuppressWarnings("unchecked")
  @Override
  public boolean isRepeatSubmit(HttpServletRequest request, RepeatSubmit annotation) {
    String nowParams = "";
    if (request instanceof RepeatedlyRequestWrapper repeatedlyRequest) {
      nowParams = HttpHelper.getBodyString(repeatedlyRequest);
    }

    // body参数为空，获取Parameter的数据
    if (StringUtils.isEmpty(nowParams)) {
      nowParams = JSONUtil.toJsonStr(request.getParameterMap());
    }
    Map<String, Object> nowDataMap = new HashMap<>();
    nowDataMap.put(REPEAT_PARAMS, nowParams);
    nowDataMap.put(REPEAT_TIME, System.currentTimeMillis());

    // 请求地址（作为存放cache的key值）
    String url = request.getRequestURI();

    // 唯一值（没有消息头则使用请求地址）
    String submitKey = StringUtils.trimToEmpty(StpUtil.getTokenValue());

    // 唯一标识（指定key + url + 消息头）
    String cacheRepeatKey = CacheConstants.REPEAT_SUBMIT_KEY + url + submitKey;

    Object sessionObj = redisCache.getCacheObject(cacheRepeatKey);
    if (sessionObj != null) {
      Map<String, Object> sessionMap = (Map<String, Object>) sessionObj;
      if (sessionMap.containsKey(url)) {
        Map<String, Object> preDataMap = (Map<String, Object>) sessionMap.get(url);
        if (compareParams(nowDataMap, preDataMap)
            && compareTime(nowDataMap, preDataMap, annotation.interval())) {
          return true;
        }
      }
    }
    Map<String, Object> cacheMap = new HashMap<String, Object>();
    cacheMap.put(url, nowDataMap);
    redisCache.setCacheObject(
        cacheRepeatKey, cacheMap, annotation.interval(), TimeUnit.MILLISECONDS);
    return false;
  }

  /** 判断参数是否相同 */
  private boolean compareParams(Map<String, Object> nowMap, Map<String, Object> preMap) {
    String nowParams = (String) nowMap.get(REPEAT_PARAMS);
    String preParams = (String) preMap.get(REPEAT_PARAMS);
    return nowParams.equals(preParams);
  }

  /** 判断两次间隔时间 */
  private boolean compareTime(
      Map<String, Object> nowMap, Map<String, Object> preMap, int interval) {
    long time1 = (Long) nowMap.get(REPEAT_TIME);
    long time2 = (Long) preMap.get(REPEAT_TIME);
    return (time1 - time2) < interval;
  }
}
